package cl.ucinf.progii.solemne1.mvc.controladores;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cl.ucinf.progii.solemne1.data.dbo.ContactoDBO;
import cl.ucinf.progii.solemne1.mvc.bean.ContactoBean;
import cl.ucinf.progii.solemne1.negocios.clases.Contacto;
import cl.ucinf.progii.solemne1.negocios.clases.Usuario;
import cl.ucinf.progii.solemne1.negocios.fabricas.UsuarioFabrica;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Login() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("user");
		String pass = request.getParameter("pass");
		
		System.out.println("user = " + user);
		System.out.println("pass = " + pass);
		
		Usuario userObj = UsuarioFabrica.getByUserAndPass(user, pass);
		System.out.println(userObj);
		if (userObj != null)
		{
			List<Contacto> contactos = userObj.getContactos();
			List<ContactoBean> contactosBean = new ArrayList();
			Iterator<Contacto> iter = contactos.iterator();
			
			while(iter.hasNext()){
				Contacto temp = iter.next();
				ContactoBean conctBean = new ContactoBean();
				conctBean.setNombre(temp.getNombre());
				conctBean.setApellido(temp.getApellido());
				contactosBean.add(conctBean);
			}
			
			System.out.println(contactos);
			request.setAttribute("ContactoBean", contactosBean);
			request.getRequestDispatcher("/exito.jsp").forward(request, response);
		}
		else
			request.getRequestDispatcher("/error.jsp").forward(request, response);
			
	
	}

}
